package Controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Dao.UserDao;
import Entity.User;

public class LoginServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public LoginServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		request.setCharacterEncoding("utf-8");
		String id=request.getParameter("cid");
		String pass=request.getParameter("cpass");
		String type=request.getParameter("ctype");
		String op=request.getParameter("op");
		if(op!=null&&op!=""&&op.equals("j")){
			request.getRequestDispatcher("/WEB-INF/page/Login.jsp").forward(request, response);
			return;
		}
		if(op!=null&&op!=""&&op.equals("exit")){
			session.invalidate();//注销
			request.getRequestDispatcher("/WEB-INF/page/Login.jsp").forward(request, response);
			return;
		}
		UserDao ser=new UserDao();
		if(type!=null&&type!=""){
			if(type.equals("1")){//学生
				User u=ser.findByNumber(id);
				if(u==null||u.getStatus()!=1){//登陆失败
					request.getRequestDispatcher("/WEB-INF/page/LoginFailure.jsp").forward(request, response);
					return;
				}
				else{
					request.setAttribute("user", u);
					session.setAttribute("uname",u.getName());
					session.setAttribute("uid", u.getNumber());
					request.setAttribute("op", "mainpage");
					request.getRequestDispatcher("AllExpServlet").forward(request, response);
					return;
				}				
			}else if(type.equals("2")){//老师
				User u=ser.findByNumber(id);
				if(u==null||u.getStatus()!=2){//登陆失败
					request.getRequestDispatcher("/WEB-INF/page/LoginFailure.jsp").forward(request, response);
					return;
				}
				else{
					//request.setAttribute("user", u);
					request.setAttribute("tname", u.getName());
					request.setAttribute("tid", u.getNumber());
					request.getRequestDispatcher("/WEB-INF/page/MyExp.jsp").forward(request, response);
					return;
				}				
			}else if(type.equals("3")){//管理员
				User u=ser.findByNumber(id);
				if(u==null||u.getStatus()!=3){//登陆失败
					request.getRequestDispatcher("/WEB-INF/page/LoginFailure.jsp").forward(request, response);
					return;
				}
				else{
					//request.setAttribute("user", u);
					request.setAttribute("mid", u.getNumber());
					request.setAttribute("mname", u.getName());
					request.getRequestDispatcher("/WEB-INF/page/ManagerPage.jsp").forward(request, response);
					return;
				}				
			}
			
		}
		
		

//		response.setContentType("text/html");
//		PrintWriter out = response.getWriter();
//		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
//		out.println("<HTML>");
//		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
//		out.println("  <BODY>");
//		out.print("    This is ");
//		out.print(this.getClass());
//		out.println(", using the GET method");
//		out.println("  </BODY>");
//		out.println("</HTML>");
//		out.flush();
//		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
